package Common 
{
	import Base.Loader.SWFLoader;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.geom.Matrix;
	
	
	public class SWFResManager
	{
		public var mainRes:Object = null;
		
		public var swfMap:Object;
		public var loader:Object;
		
		public var loadCount:int = 0;
		
		public var bdObject:Object = null;
		public var lineArray:Array = null;
		
		public var object:Object = null;
		public var maxNum:int = 0;
		public var s:int = 0;
		
		public function SWFResManager()
		{
			
		}
		
		
		public static var instance:SWFResManager = null;
		public static function Instance():SWFResManager
		{
			if ( instance == null )
			{
				instance = new SWFResManager();
			}
			
			return instance;
		}
		
		
		public function setObject( obj:Object ):void
		{
			object = obj;
		}
		
		
		public function init():void
		{
			swfMap = new Object();
			loader = new Object();
			
			bdObject = new Object();
			
			lineArray = new Array();
		}
		
		
		public function release():void
		{
			swfMap = null;
		}
		
		
		public function getSWF( path:String ):MovieClip
		{
			if ( swfMap[ path ] )
			{
				return swfMap[ path ].content;
			}
			
			return null;
		}
		
		
		public function getClass( path:String , name:String ):Class
		{
			if ( swfMap[ path ] )
			{
				return swfMap[ path ].getClass( name );
			}
			
			return null;
		}
		
		
		public function clear():void
		{
			while( lineArray.length )
			{
				var path:String = lineArray[ 0 ].loader.path;
				
				loader[ path ].loader.contentLoaderInfo.removeEventListener( Event.INIT , onInit );
				loader[ path ].loader.contentLoaderInfo.removeEventListener( IOErrorEvent.IO_ERROR , onError );
				loader[ path ].loader.contentLoaderInfo.removeEventListener( SecurityErrorEvent.SECURITY_ERROR , onSecurityError );
				loader[ path ].loader.contentLoaderInfo.removeEventListener( ProgressEvent.PROGRESS , onProgress );
				loader[ path ].loader.Release();
				
				delete loader[ path ].loader;
				delete loader[ path ].fun;
				delete loader[ path ];
				
				lineArray.shift();
			}
			
			loadCount = 0;
			UILoading.instance.visible( false );
		}
		
		
		public function unload( path:String ):void
		{
			if ( loader[ path ] )
			{
				for ( var i:int = 0 ; i < lineArray.length ; i++ )
				{
					if ( lineArray[ i ].loader.path == path )
					{
						lineArray.splice( i , 1 );
						break;
					}
				}
				
				loader[ path ].loader.contentLoaderInfo.removeEventListener( Event.INIT , onInit );
				loader[ path ].loader.contentLoaderInfo.removeEventListener( IOErrorEvent.IO_ERROR , onError );
				loader[ path ].loader.contentLoaderInfo.removeEventListener( SecurityErrorEvent.SECURITY_ERROR , onSecurityError );
				loader[ path ].loader.contentLoaderInfo.removeEventListener( ProgressEvent.PROGRESS , onProgress );
				loader[ path ].loader.Release();
				
				delete loader[ path ].loader;
				delete loader[ path ].fun;
				delete loader[ path ];
				
				loadCount--;
				
				
				
				//lineArray.shift();
				
				// 这里可能有问题，
				
				//if ( !loadCount )
				//{
				
				
				//}
				
				var b:Boolean = false;
				for ( var j:int = 0 ; j < lineArray.length ; j++ )
				{
					if ( lineArray[ j ].loading )
					{
						b = true;
					}
				}
				
				UILoading.instance.visible( b );
				
				if ( lineArray.length && i == 0 )
				{
					var path:String = lineArray[ 0 ].loader.path;
					lineArray[ 0 ].loader.Load( path );
					lineArray[ 0 ].loader.path = path;
					
					UILoading.instance.visible( lineArray[ 0 ].loading );
				}
				
			}
		}
		
		
		public function clearLine():void
		{
			while( lineArray.length )
			{
				lineArray.shift();
			}
			
			loadCount = 0;
		}
		
		
		public function load( path:String , fun:Function , loading:Boolean = true , loadingBig:Boolean = false ):void
		{
			//loading = true;
			
			if ( loader[ path ] )
			{
				loader[ path ].loading = loading;
				loader[ path ].loadingBig = loadingBig;
				loader[ path ].fun.push( fun );
				return;
			}
			
			if ( swfMap[ path ] )
			{
				fun();
				return;
			}
			
			if ( object )
			{
				object.stopLoadBg();
			}
			
			
			loadCount++;
			
			var l:SWFLoader = new SWFLoader();
			l.contentLoaderInfo.addEventListener( Event.INIT , onInit );
			l.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR , onError );
			l.contentLoaderInfo.addEventListener( SecurityErrorEvent.SECURITY_ERROR , onSecurityError );
			l.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS , onProgress );
			
			l.path = path;
			
			loader[ path ] = new Object();
			loader[ path ].loader = l;
			loader[ path ].loading = loading;
			loader[ path ].loadingBig = loadingBig;
			loader[ path ].fun = new Array();
			loader[ path ].fun.push( fun );
			
			lineArray.push( loader[ path ] );
			
			if ( lineArray.length == 1 )
			{
				l.Load( path );
			}
			
			if ( loading )
			{
				//
				UILoading.instance.visible( true );
				UILoading.instance.setText( 0 , loadCount , "" , s );
			}
		}
		
		
		public function onSecurityError( event:SecurityErrorEvent ):void
		{
			loadCount--;
			
			lineArray.shift();
			
			var b:Boolean = false;
			for ( var i:int = 0 ; i < lineArray.length ; i++ )
			{
				if ( lineArray[ i ].loading )
				{
					b = true;
				}
			}
			UILoading.instance.visible( b );
			
			if ( lineArray.length )
			{
				var path:String = lineArray[ 0 ].loader.path;
				lineArray[ 0 ].loader.Load( path );
				lineArray[ 0 ].loader.path = path;
				
				UILoading.instance.visible( lineArray[ 0 ].loading );
			}
			else
			{
				if ( object )
				{
					object.startLoadBg();
				}
			}
			
			//UILoading.instance.setText( 100  , loadCount , event.target.loader.path + "serror" );
			//UILoading.instance.setText( 100  , loadCount , "" );
			
			//if ( !loadCount )
			//{
			
			//}
			
			event.target.loader.contentLoaderInfo.removeEventListener( Event.INIT , onInit );
			event.target.loader.contentLoaderInfo.removeEventListener( IOErrorEvent.IO_ERROR , onError );
			event.target.loader.contentLoaderInfo.removeEventListener( SecurityErrorEvent.SECURITY_ERROR , onSecurityError );
			event.target.loader.contentLoaderInfo.removeEventListener( ProgressEvent.PROGRESS , onProgress );
			
			
			delete loader[ event.target.loader.path ].loader;
			delete loader[ event.target.loader.path ].fun;
			delete loader[ event.target.loader.path ];
		}
		
		
		public function onError( event:IOErrorEvent ):void
		{
			loadCount--;
			
			lineArray.shift();
			
			var b:Boolean = false;
			for ( var i:int = 0 ; i < lineArray.length ; i++ )
			{
				if ( lineArray[ i ].loading )
				{
					b = true;
				}
			}
			UILoading.instance.visible( b );
			
			if ( lineArray.length )
			{
				var path:String = lineArray[ 0 ].loader.path;
				lineArray[ 0 ].loader.Load( path );
				lineArray[ 0 ].loader.path = path;
				
				UILoading.instance.visible( lineArray[ 0 ].loading );
			}
			else
			{
				if ( object )
				{
					object.startLoadBg();
				}
			}
			
			//UILoading.instance.setText( 100  , loadCount , event.target.loader.path + "error" );
			
			//if ( !loadCount )
			//{
			
			//}
			
			event.target.loader.contentLoaderInfo.removeEventListener( Event.INIT , onInit );
			event.target.loader.contentLoaderInfo.removeEventListener( IOErrorEvent.IO_ERROR , onError );
			event.target.loader.contentLoaderInfo.removeEventListener( SecurityErrorEvent.SECURITY_ERROR , onSecurityError );
			event.target.loader.contentLoaderInfo.removeEventListener( ProgressEvent.PROGRESS , onProgress );
			
			
			delete loader[ event.target.loader.path ].loader;
			delete loader[ event.target.loader.path ].fun;
			delete loader[ event.target.loader.path ];
		}
		
		
		public function onProgress( event:ProgressEvent ):void
		{
			
			//if ( loader[ event.target.loader.path ].loading )
			//{
			//			var c:int = 0;
			//			var t:String = "";
			//			for each ( var obj:Object in loader )
			//			{
			//				t += obj.loader.path;
			//			}
			//			
			
			var n:int = 0;
			for ( var i:int = 0 ; i < lineArray.length ; i++ )
			{
				if ( lineArray[ i ].loading )
				{
					n++;
				}
			}
			var num:int = event.bytesLoaded / event.bytesTotal * 100;
			
			UILoading.instance.setText( num  , n , "" , s );
			
			if ( loader[ event.target.loader.path ].loadingBig )
			{
				var num1:int = 0;
				for ( i = 0 ; i < lineArray.length ; i++ )
				{
					if ( lineArray[ i ].loadingBig )
					{
						num1++;
					}
				}
				
				UILoading.instance.visible( false );
				GameLoading.instance.loading( event.target.loader.path , num , maxNum ? maxNum - num1 : 0 , maxNum );
			}
			else if ( loader[ event.target.loader.path ].loading )
			{
				UILoading.instance.visible( true );
			}
			
			//UILoading.instance.setText( num  , loadCount , event.bytesLoaded.toString() + "/" + event.bytesTotal.toString() + "-" + t );
			//}
			
			
			
		}
		
		
		public function onInit( event:Event ):void
		{
			loadCount--;
			
			lineArray.shift();
			
			var b:Boolean = false;
			for ( var i:int = 0 ; i < lineArray.length ; i++ )
			{
				if ( lineArray[ i ].loading )
				{
					b = true;
				}
			}
			UILoading.instance.visible( b );
			
			if ( lineArray.length )
			{
				var path:String = lineArray[ 0 ].loader.path;
				lineArray[ 0 ].loader.Load( path );
				lineArray[ 0 ].loader.path = path;
				
				UILoading.instance.visible( lineArray[ 0 ].loading );
			}
			else
			{
				if ( object )
				{
					object.startLoadBg();
				}
			}
			
			
			//if ( !loadCount )
			//{
			
			//}
			
			swfMap[ event.target.loader.path ] = loader[ event.target.loader.path ].loader;
			
			for ( i = 0 ; i < loader[ event.target.loader.path ].fun.length ; i++ )
			{
				if ( loader[ event.target.loader.path ].fun[ i ] != null )
				{
					loader[ event.target.loader.path ].fun[ i ]( loader[ event.target.loader.path ].loader );
				}
			}
			
			event.target.loader.contentLoaderInfo.removeEventListener( Event.INIT , onInit );
			event.target.loader.contentLoaderInfo.removeEventListener( IOErrorEvent.IO_ERROR , onError );
			event.target.loader.contentLoaderInfo.removeEventListener( SecurityErrorEvent.SECURITY_ERROR , onSecurityError );
			event.target.loader.contentLoaderInfo.removeEventListener( ProgressEvent.PROGRESS , onProgress );
			
			
			delete loader[ event.target.loader.path ].loader;
			delete loader[ event.target.loader.path ].fun;
			delete loader[ event.target.loader.path ];
			
			//			var t:String = "";
			//			for each ( var obj:Object in loader )
			//			{
			//				t += obj.loader.path;
			//			}
			//			UILoading.instance.setText( 100  , loadCount , t + " init" );
		}
		
	}
}
